package com.shelpe.services.algorithm.choose;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shelpe.services.algorithm.entity.jpa.Adgroup;
import com.shelpe.services.algorithm.entity.jpa.Campaign;
import com.shelpe.services.algorithm.repository.jpa.CampaignRepository;

@Service
public class MobileQscoreChooseHandler implements ChooseHandler<Adgroup>, InitializingBean {
	@Autowired
	private CampaignRepository campaignRepository;
	
	private int[] mappings = new int[0];
	
	@Override
	public int count(ChooseHandler.ChooseHandleContext<Adgroup> context) {
		Campaign campaign = this.campaignRepository.findOne(context.getEntity().getCampaignID());
		int index = (campaign.getBudget() - 30) / 20;
		return (mappings.length > index && index > -1)?this.mappings[index]: 50;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		mappings = new int[5];
		mappings[0] = 10;
		mappings[1] = 20;
		mappings[2] = 30;
		mappings[3] = 40;
		mappings[4] = 50;
	}

	@Override
	public boolean support(ChooseHandler.ChooseHandleContext<Adgroup> context) {
		return context.getEntity().getDriveType() == 2;
	}

	
}
